package com.yp.shopping.controller;

import com.yp.shopping.bean.Result;
import com.yp.shopping.model.Category;
import com.yp.shopping.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //添加商品类别
    @RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
    @ResponseBody
    public Result saveCategory(@RequestBody Category category) {

        return categoryService.saveCategory(category);
    }

    //获取所有商品类别
    @RequestMapping(value = "/getCategories", method = RequestMethod.GET)
    @ResponseBody
    public List<Category> getCategories() {

        return categoryService.getCategories();
    }

    //通过id获取商品类别
    @RequestMapping(value = "/getCategoryById", method = RequestMethod.GET)
    @ResponseBody
    public Category getCategoryById(Integer id) {

        return categoryService.getCategoryById(id);
    }

    //通过id删除商品类别
    @RequestMapping(value = "/deleteCategoryById", method = RequestMethod.GET)
    @ResponseBody
    public Result deleteCategoryById(Integer id) {

        return categoryService.deleteCategoryById(id);
    }

    //更新商品类别
    @RequestMapping(value = "/updateCategory", method = RequestMethod.POST)
    @ResponseBody
    public Category updateCategory(@RequestBody Category category) {

        return categoryService.updateCategory(category);
    }


}
